<?php

/**
 * Created by Zhuma
 * User: 
 * Date: 2016-06-29
 * Time: 08:46
 */

namespace Partner\Model;

class Export {

    public function export($data,$header) {
        $num = count($header);
        $letter = array_slice(array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'), 0, $num);

        ob_clean();
        Vendor("Classes.PHPExcel");
        $objPHPExcel = new \PHPExcel();
        for ($i = 0; $i < $num; $i++) {
            $objPHPExcel->getActiveSheet()->setCellValue("$letter[$i]1", "$header[$i]");
        }
        //dump($objPHPExcel);die;
        for ($i = 2; $i <= count($data) + 1; $i++) {
            $j = 0;
            foreach ($data[$i - 2] as $value) {
                $objPHPExcel->getActiveSheet()->setCellValue("$letter[$j]$i", "$value");
                $j++;
            }
        }
        
        Vendor("Classes.PHPExcel.IOFactory");
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        $ua = $_SERVER["HTTP_USER_AGENT"];
        if (preg_match("/MSIE/", $ua)) {
            header('Content-Disposition: attachment; filename="' . time() . '.xls"');
        } else if (preg_match("/Firefox/", $ua)) {
            header('Content-Disposition: attachment; filename*="utf8\'\'' . time() . '.xls"');
        } else {
            header('Content-Disposition: attachment; filename="' . time() . '.xls"');
        }

        header("Content-Transfer-Encoding:binary");
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save('php://output');
        die;
    }

}
